package com.mayuan.controller;

import com.mayuan.domain.ResponseResult;
import com.mayuan.domain.dto.RoleListDto;
import com.mayuan.domain.dto.RoleUpdateDto;
import com.mayuan.domain.entity.Role;
import com.mayuan.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    public ResponseResult list(Integer pageNum, Integer pageSize, RoleListDto roleListDto){
        //查询角色列表
        return roleService.pageRoleList(pageNum,pageSize,roleListDto);
    }

    @PutMapping("/changeStatus")
    public ResponseResult changeStatus(@RequestBody RoleUpdateDto roleUpdateDto){
        //改变角色状态
        return roleService.changeStatus(roleUpdateDto);
    }

    /**
     * 新增角色
     */
    @PostMapping
    public ResponseResult add( @RequestBody Role role)
    {
        roleService.insertRole(role);
        return ResponseResult.okResult();

    }

    /**
     * 根据角色编号获取详细信息
     */
    @GetMapping("/{roleId}")
    public ResponseResult getInfo(@PathVariable("roleId") Long roleId){
        Role role = roleService.getById(roleId);
        return ResponseResult.okResult(role);
    }

    /**
     * 修改并保存角色
     */
    @PutMapping
    public ResponseResult edit(@RequestBody Role role)
    {
        roleService.updateRole(role);
        return ResponseResult.okResult();
    }

    /**
     * 删除角色
     * @param id
     */
    @DeleteMapping("/{id}")
    public ResponseResult remove(@PathVariable("id") Long id) {
        roleService.removeById(id);
        return ResponseResult.okResult();
    }

    /**
     * 查询角色列表
     * @return
     */
    @GetMapping("/listAllRole")
    public ResponseResult listAllRole(){
        List<Role> roles = roleService.selectRoleAll();
        return ResponseResult.okResult(roles);
    }
}
